﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 美妆产品管理系统.Department
{
    public partial class Update_department : Form
    {
        public Update_department()
        {
            InitializeComponent();
        }

        SqlDataAdapter dadepartment;
        DataSet ds = new DataSet();
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtID.Text = dgvUpdatedepartment.CurrentRow.Cells["Department_ID"].Value.ToString();
            txtname.Text = dgvUpdatedepartment.CurrentRow.Cells["Department_Name"].Value.ToString();
            txtmanager.Text = dgvUpdatedepartment.CurrentRow.Cells["Department_Manager"].Value.ToString();
            txtdescn.Text = dgvUpdatedepartment.CurrentRow.Cells["Department_Descn"].Value.ToString();
        }


        void init()
        {
            DB.GetCn();
            string str = "select * from Department";
            dadepartment = new SqlDataAdapter(str, DB.cn);
            dadepartment.Fill(ds, "department_info");
            DB.cn.Close();
        }

        void showAlldepartment()
        {
            DataView dvDepartment = new DataView(ds.Tables["department_info"]);
            dgvUpdatedepartment.DataSource = dvDepartment;
            dgvUpdatedepartment.Columns[3].Width = 500;
        }

        void dgvHead()
        {
            this.dgvUpdatedepartment.Columns[0].HeaderText = "部门编号";
            this.dgvUpdatedepartment.Columns[1].HeaderText = "部门名称";
            this.dgvUpdatedepartment.Columns[2].HeaderText = "部门经理";
            this.dgvUpdatedepartment.Columns[3].HeaderText = "部门描述";
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Update_department_Load(object sender, EventArgs e)
        {
            init();
            showAlldepartment();
            dgvHead();
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            if (txtID.Text == "" || txtname.Text == "")
            {
                MessageBox.Show("部门编号或部门名称不能为空");
            }
            else
            {
                DB.GetCn();
                DialogResult dr = MessageBox.Show("您确定要进行修改吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if(dr == DialogResult.OK)
                {
                    int a = dgvUpdatedepartment.CurrentRow.Index;
                    string srt = dgvUpdatedepartment.Rows[a].Cells["Department_ID"].Value.ToString();
                    DataRow[] DepRows = ds.Tables["department_info"].Select("Department_ID='" + srt + "'");
                    DepRows[0]["Department_Name"] = txtname.Text.Trim();
                    DepRows[0]["Department_ID"] = txtID.Text.Trim();
                    DepRows[0]["Department_Manager"] = txtmanager.Text.Trim();
                    DepRows[0]["Department_Descn"] = txtdescn.Text.Trim();
                    MessageBox.Show("修改成功");
                }
                else
                {
                    return;
                }

                //使用存储过程添加日志信息
                SqlCommand cmd = new SqlCommand("add_log", DB.cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("username", SqlDbType.NVarChar));
                cmd.Parameters.Add(new SqlParameter("type", SqlDbType.NVarChar));
                cmd.Parameters.Add(new SqlParameter("action_date", SqlDbType.DateTime));
                cmd.Parameters.Add(new SqlParameter("action_table", SqlDbType.NVarChar));

                cmd.Parameters["username"].Value = Login.username;
                cmd.Parameters["type"].Value = "修改";
                cmd.Parameters["action_date"].Value = DateTime.Now;
                cmd.Parameters["action_table"].Value = "Department表";
                try
                {
                    SqlCommandBuilder dbEmployee = new SqlCommandBuilder(dadepartment);
                    dadepartment.Update(ds, "department_info");
                    cmd.ExecuteNonQuery();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                DB.cn.Close();
            }
        }

        private void btnCance_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
